package com.skynet.whwpro.manager;

import com.skynet.whwpro.Alipay.UserAuthController;
import com.skynet.whwpro.bean.BeanDyb;
import com.skynet.whwpro.bean.BeanSjzcb;
import com.skynet.whwpro.utils.QueryColResult;
import com.skynet.whwpro.utils.Utils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.filter.CrossOriginFilter;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;

import java.io.File;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@IocBean
@At("/query")
@Filters(@By(type = CrossOriginFilter.class))
public class ActionQuery {

    @Inject
    private Dao dao;


    @At
    @Ok("json")
    public QueryResult getSpList(@Param("page") int pageNum, @Param("rowsPerPage") int pageSize) {
        Pager pager = dao.createPager(pageNum, pageSize);
        Cnd cnd = Cnd.NEW();
        cnd.and("zt", "=", "1").desc("pgrksj");
        pager.setRecordCount(dao.count(BeanSjzcb.class, cnd));
        return new QueryResult(dao.query(BeanSjzcb.class, cnd, pager), pager);
    }

    @At
    @Ok("json")
    public QueryColResult getClassList(@Param("page") int pageNum, @Param("rowsPerPage") int pageSize,
                                       @Param("name") String name, @Param("sortBy") String sortBy,
                                       @Param("query") String query) {
        Pager pager = dao.createPager(pageNum, pageSize);
        Cnd cnd = Cnd.NEW();
        if (Strings.isBlank(sortBy)) {
            sortBy = "pgrksj";
        }
        cnd.and("zt", "=", "1").desc(sortBy);
        Class<?> clas = this.queryClass(name);
        String tableName = this.queryClassName(name);
        String sqls = "(select col.column_name, col.data_type, des.description from information_schema.columns col left join pg_description des on col.table_name::regclass = des.objoid and col.ordinal_position = des.objsubid where table_schema = 'public' and table_name = '" + tableName + "' order by ordinal_position)";
        Sql sl = Sqls.create(sqls);
        sl.setCallback(Sqls.callback.records());
        sl.forceExecQuery();
        dao.execute(sl);
        pager.setRecordCount(dao.count(clas, cnd));
        List<Record> colconfig = sl.getList(Record.class);
        if (Strings.isNotBlank(query)) {
            SqlExpressionGroup sg=new SqlExpressionGroup();
            for (Record rd : colconfig) {
                String column_name = rd.getString("column_name");
                sg.or(column_name+"::text", "like", "%" + query + "%");
            }
            cnd.and(sg);
        }
        return new QueryColResult(dao.query(clas, cnd, pager), colconfig, pager);
    }

    @At
    @Ok("json")
    public String queryClassName(@Param("className") String className) {
        String packageName = "com.skynet.whwpro.bean.";
        // List<String> classNames = getClassName(packageName);
        String ret = "";
        try {
            Class clazz = Class.forName(packageName + className);
            System.out.println(clazz.getSimpleName());
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation an : annotations) {
                String regex = "value=(.+?),";
                Matcher matcher = Pattern.compile(regex).matcher(an.toString());
                while (matcher.find()) {
                    ret = matcher.group(1);
                }
            }
            return ret;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return "";
        }

    }

    @At
    @Ok("json")
    public Class<?> queryClass(@Param("className") String className) {
        String packageName = "com.skynet.whwpro.bean.";

        try {
            Class clazz = Class.forName(packageName + className);
            return clazz;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }

    }
}
